package com.base.web.core.util.jedis.impl;
import com.base.web.core.util.jedis.JedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description: redis单机操作实现
 *
 * @Author: rentie
 * @Date: 2018/7/11 10:39
 */
@Service("JedisServiceImpl")
public class JedisServiceImpl implements JedisService {

    private Logger logger = LoggerFactory.getLogger(JedisServiceImpl.class);


    @Autowired
    private JedisPool jedisPool;

    /**
     * 获得redis连接
     *
     * @return
     */
    private Jedis getResource() {
        return jedisPool.getResource();
    }

    /**
     * 保存数据到缓存
     *
     * @param key
     * @param value
     * @return
     */
    @Override
    public Boolean set(String key, String value) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.set(key, value);
            result = true;
        } catch (Exception e) {
            logger.error("redis保存数据异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 保存数据到缓存并设置过期时间
     *
     * @param key
     * @param value
     * @param time
     * @return
     */
    @Override
    public Boolean setex(String key, int time, String value) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.setex(key, time, value);
            result = true;
        } catch (Exception e) {
            logger.error("redis保存数据并设置过期时间异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 获得缓存中的数据
     *
     * @param key
     * @return
     */
    @Override
    public String get(String key) {
        Jedis jedis = getResource();
        String result = "";
        try {
            result = jedis.get(key);
        } catch (Exception e) {
            logger.error("获取redis缓存数据异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 删除键
     *
     * @param key
     * @return
     */
    @Override
    public Boolean del(String key) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.del(key);
            result = true;
        } catch (Exception e) {
            logger.error("删除redis缓存异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 设置某个缓存的过期时间（以秒为单位）
     *
     * @param key  键值
     * @param time 缓存时间
     */
    @Override
    public Boolean expire(String key, int time) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.expire(key, time);
            result = true;
        } catch (Exception e) {
            logger.error("设置redis缓存过期时间异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 移除给定key过期时间(设置这个key永不过期)
     *
     * @param key
     */
    @Override
    public Boolean persist(String key) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.persist(key);
            result = true;
        } catch (Exception e) {
            logger.error("移除" + key + "过期时间异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 判断某个键是否存在
     *
     * @param key
     * @return
     */
    @Override
    public Boolean exists(String key) {
        Jedis jedis = getResource();
        try {
            return jedis.exists(key);
        } finally {
            jedis.close();
        }

    }

    /**
     * 查找特定的key(*为查找所有)
     *
     * @param key
     * @return
     */
    @Override
    public Set<String> keys(String key) {
        Jedis jedis = getResource();
        try {
            return jedis.keys(key);
        } finally {
            jedis.close();
        }
    }

    /**
     * 返回key所储存的值的类型
     *
     * @param key
     * @return
     */
    @Override
    public String type(String key) {
        Jedis jedis = getResource();
        try {
            return jedis.type(key);
        } finally {
            jedis.close();
        }
    }

    /**
     * 清空所有数据库的key
     */
    @Override
    public Boolean flushAll() {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.flushAll();
            result = true;
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 清空当前数据库的key
     */
    @Override
    public Boolean flushDB() {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.flushDB();
            result = true;
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 返回key的个数
     *
     * @return
     */
    @Override
    public Long dbSize() {
        Jedis jedis = getResource();
        try {
            return jedis.dbSize();
        } finally {
            jedis.close();
        }
    }

    /**
     * List操作 左入栈
     *
     * @param key
     * @param value
     */
    @Override
    public Boolean lpush(String key, String... value) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.lpush(key, value);
            result = true;
        } catch (Exception e) {
            logger.error("左入栈异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * List操作 左出栈
     *
     * @param key
     * @return
     */
    @Override
    public String lpop(String key) {
        Jedis jedis = getResource();
        String result = "";
        try {
            result = jedis.lpop(key);
        } catch (Exception e) {
            logger.error("左出栈异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * List操作  右入栈
     *
     * @param key
     * @param value
     */
    @Override
    public Boolean rpush(String key, String... value) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.rpush(key, value);
            result = true;
        } catch (Exception e) {
            logger.error("右入栈异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * List操作  右出栈
     *
     * @param key
     * @return
     */
    @Override
    public String rpop(String key) {
        Jedis jedis = getResource();
        String result = "";
        try {
            result = jedis.rpop(key);
        } catch (Exception e) {
            logger.error("右出栈异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 获得List元素个数
     *
     * @param key
     * @return
     */
    @Override
    public Long llen(String key) {
        Jedis jedis = getResource();
        try {
            return jedis.llen(key);
        } finally {
            jedis.close();
        }
    }

    /**
     * Set操作 set添加一个或多个数据
     *
     * @param key
     * @param value
     */
    @Override
    public Boolean sadd(String key, String... value) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.sadd(key, value);
            result = true;
        } catch (Exception e) {
            logger.error("redis set添加数据异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * Set操作 获得所有元素
     *
     * @param key
     * @return
     */
    @Override
    public Set<String> smembers(String key) {
        Jedis jedis = getResource();
        Set<String> result = null;
        try {
            result = jedis.smembers(key);
        } catch (Exception e) {
            logger.error("redis set添加数据异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 删除set中指定一个或多个元素
     *
     * @param key
     * @param value
     */
    @Override
    public Boolean srem(String key, String... value) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.srem(key, value);
            result = true;
        } catch (Exception e) {
            logger.error("redis set删除数据异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 随机Set中的一个元素
     *
     * @param key
     */
    @Override
    public String spop(String key) {
        Jedis jedis = getResource();
        try {
            return jedis.spop(key);
        } finally {
            jedis.close();
        }
    }

    /**
     * 获得Set中的元素个数
     *
     * @param key
     * @return
     */
    @Override
    public Long scard(String key) {
        Jedis jedis = getResource();
        try {
            return jedis.scard(key);
        } finally {
            jedis.close();
        }
    }

    /**
     * 散列hash保存Map
     *
     * @param key
     * @param map
     */
    @Override
    public Boolean hmset(String key, Map<String, String> map) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.hmset(key, map);
            result = true;
        } catch (Exception e) {
            logger.error("散列hash保存Map数据异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 散列hash 单个值保存
     *
     * @param key   缓存键值
     * @param field 映射表键
     * @param value 映射表值
     */
    @Override
    public Boolean hset(String key, String field, String value) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.hset(key, field, value);
            result = true;
        } catch (Exception e) {
            logger.error("散列hash保存单个数据异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 散列hash获得所有键值对
     *
     * @param key
     * @return
     */
    @Override
    public Map<String, String> hgetAll(String key) {
        Jedis jedis = getResource();
        Map<String, String> result = null;
        try {
            result = jedis.hgetAll(key);
        } catch (Exception e) {
            logger.error("散列hash获得所有键值对异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 散列hash获得所有键
     *
     * @param key
     * @return
     */
    @Override
    public Set<String> hkeys(String key) {
        Jedis jedis = getResource();
        Set<String> result = null;
        try {
            result = jedis.hkeys(key);
        } catch (Exception e) {
            logger.error("散列hash获得所有键异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 散列hash获得所有值
     *
     * @param key
     * @return
     */
    @Override
    public List<String> hvals(String key) {
        Jedis jedis = getResource();
        List<String> result = null;
        try {
            result = jedis.hvals(key);
        } catch (Exception e) {
            logger.error("散列hash获得所有值异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 删除一个或多个键值对
     *
     * @param key   缓存键
     * @param field 映射表键
     */
    @Override
    public Boolean hdel(String key, String... field) {
        Jedis jedis = getResource();
        Boolean result = false;
        try {
            jedis.hdel(key, field);
            result = true;
        } catch (Exception e) {
            logger.error("散列hash删除一个键值对异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }

    /**
     * 判断hash中是否存在某个键值对
     *
     * @param key   缓存键
     * @param field 映射表键
     * @return
     */
    @Override
    public Boolean hexists(String key, String field) {
        Jedis jedis = getResource();
        try {
            return jedis.hexists(key, field);
        } finally {
            jedis.close();
        }
    }

    /**
     * 获取hash中的指定键值
     *
     * @param key   缓存键
     * @param field 映射表键
     * @return
     */
    @Override
    public List<String> hmget(String key, String... field) {
        Jedis jedis = getResource();
        List<String> result = null;
        try {
            result = jedis.hmget(key, field);
        } catch (Exception e) {
            logger.error("散列hash获得指定键值异常:" + e.getMessage());
        } finally {
            jedis.close();
        }
        return result;
    }
}